package main;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.FileNotFoundException;

import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JFrame;

import org.scilab.modules.javasci.JavasciException;
import org.scilab.modules.javasci.Scilab;
import org.scilab.modules.javasci.JavasciException.InitializationException;
import org.scilab.modules.types.ScilabType;

import test.MyPrintStream;
import utils.Contents;
import utils.MessageBox;

/*
 * DingY.java
 *
 * Created on __DATE__, __TIME__
 */

/**
 *
 * @author  __USER__
 */

public class DingY extends javax.swing.JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = -4181318942252695802L;
	/** Creates new form DingY */
	private DingY() {
		initComponents();
		redirect();

	}
	private static DingY instance;
	public static DingY getInstance(){
		if(instance==null){
			instance =  new DingY();
		}
		return instance;
	}
	public void redirect() {
		try {
			//scilab初始化 并开启
			sci = new Scilab();
			sci.open();
			//初始化输出系统
			mps = new MyPrintStream(System.out, jTextArea1);
			System.setOut(mps);
			System.setErr(mps);
			
		} catch (InitializationException e) {
			MessageBox.Show("Scilab 初始化失败");
			e.printStackTrace();
			
		} catch (JavasciException e) {
			MessageBox.Show("Scilab 计算引擎 打开失败！");
			e.printStackTrace();
		}catch(Exception e){
			MessageBox.Show("输出重定向发生错误");
		}

	}

	//GEN-BEGIN:initComponents
	// <editor-fold defaultstate="collapsed" desc="Generated Code">
	private void initComponents() {
		getContentPane().setBackground(new Color(199, 191, 230));
		ImageIcon icon=new ImageIcon(Contents.ICON_PATH);//
		setIconImage(icon.getImage());
		jButton1 = new javax.swing.JButton();
		jScrollPane1 = new javax.swing.JScrollPane();
		jTextArea1 = new javax.swing.JTextArea();
		jButton2 = new javax.swing.JButton();
		menuBar = new javax.swing.JMenuBar();
		fileMenu = new javax.swing.JMenu();
		openMenuItem = new javax.swing.JMenuItem();
		exitMenuItem = new javax.swing.JMenuItem();
		helpMenu = new javax.swing.JMenu();
		contentsMenuItem = new javax.swing.JMenuItem();
		aboutMenuItem = new javax.swing.JMenuItem();
		jScrollPane2 = new javax.swing.JScrollPane();
		jTextArea2 = new javax.swing.JTextArea();
		menuBar1 = new javax.swing.JMenuBar();
		fileMenu1 = new javax.swing.JMenu();
		openMenuItem1 = new javax.swing.JMenuItem();
		exitMenuItem1 = new javax.swing.JMenuItem();
		helpMenu1 = new javax.swing.JMenu();
		contentsMenuItem1 = new javax.swing.JMenuItem();
		aboutMenuItem1 = new javax.swing.JMenuItem();

		setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

		jButton1.setText("\u6267\u884c");
		jButton1.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				jButton1ActionPerformed(evt);
			}
		});
		openMenuItem1.addActionListener(openfile);
		jTextArea1.setColumns(20);
		jTextArea1.setRows(5);
		jScrollPane1.setViewportView(jTextArea1);

		jButton2.setText("\u6e05\u7a7a\u8f93\u51fa");
		jButton2.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				jButton2ActionPerformed(evt);
			}
		});

		fileMenu.setText("\u6587\u4ef6");

		openMenuItem.setText("\u6253\u5f00");
		fileMenu.add(openMenuItem);

		exitMenuItem.setText("\u9000\u51fa");
		fileMenu.add(exitMenuItem);

		menuBar.add(fileMenu);

		helpMenu.setText("\u5e2e\u52a9");

		contentsMenuItem.setText("Contents");
		helpMenu.add(contentsMenuItem);

		aboutMenuItem.setText("About");
		helpMenu.add(aboutMenuItem);

		menuBar.add(helpMenu);

		jTextArea2.setColumns(20);
		jTextArea2.setRows(5);
		jScrollPane2.setViewportView(jTextArea2);

		fileMenu1.setText("\u6587\u4ef6");

		openMenuItem1.setText("\u6253\u5f00");
		fileMenu1.add(openMenuItem1);

		exitMenuItem1.setText("\u9000\u51fa");
		
		fileMenu1.add(exitMenuItem1);

		menuBar1.add(fileMenu1);

		helpMenu1.setText("\u5e2e\u52a9");

		contentsMenuItem1.setText("Contents");
		helpMenu1.add(contentsMenuItem1);

		aboutMenuItem1.setText("About");
		helpMenu1.add(aboutMenuItem1);

		menuBar1.add(helpMenu1);

		setJMenuBar(menuBar1);

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(
				getContentPane());
		getContentPane().setLayout(layout);
		layout
				.setHorizontalGroup(layout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								javax.swing.GroupLayout.Alignment.TRAILING,
								layout
										.createSequentialGroup()
										.addGroup(
												layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.TRAILING)
														.addGroup(
																javax.swing.GroupLayout.Alignment.LEADING,
																layout
																		.createSequentialGroup()
																		.addContainerGap()
																		.addComponent(
																				jButton2))
														.addGroup(
																javax.swing.GroupLayout.Alignment.LEADING,
																layout
																		.createSequentialGroup()
																		.addGap(
																				22,
																				22,
																				22)
																		.addComponent(
																				jButton1))
														.addGroup(
																javax.swing.GroupLayout.Alignment.LEADING,
																layout
																		.createSequentialGroup()
																		.addContainerGap()
																		.addGroup(
																				layout
																						.createParallelGroup(
																								javax.swing.GroupLayout.Alignment.TRAILING,
																								false)
																						.addComponent(
																								jScrollPane1,
																								javax.swing.GroupLayout.Alignment.LEADING)
																						.addComponent(
																								jScrollPane2,
																								javax.swing.GroupLayout.Alignment.LEADING,
																								javax.swing.GroupLayout.DEFAULT_SIZE,
																								560,
																								Short.MAX_VALUE))))
										.addContainerGap()));
		layout
				.setVerticalGroup(layout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								layout
										.createSequentialGroup()
										.addComponent(
												jScrollPane2,
												javax.swing.GroupLayout.PREFERRED_SIZE,
												106,
												javax.swing.GroupLayout.PREFERRED_SIZE)
										.addGap(3, 3, 3)
										.addComponent(jButton1)
										.addGap(27, 27, 27)
										.addComponent(
												jScrollPane1,
												javax.swing.GroupLayout.DEFAULT_SIZE,
												175, Short.MAX_VALUE)
										.addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.RELATED)
										.addComponent(jButton2)
										.addContainerGap()));
		addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e){
				sci.close();
				mps.clear();
				this.windowClosed(null);
			}
		});
		pack();
	}// </editor-fold>
	//GEN-END:initComponents

	public  ActionListener openfile = new  ActionListener() {
		
		 public void actionPerformed(ActionEvent e){
			    String msg;
			    JFileChooser  fDialog=new JFileChooser(); //文件选择器
			    frame = new JFrame();
			    int result=fDialog.showOpenDialog(frame);
			    if(result==JFileChooser.APPROVE_OPTION){
				     msg="成功加载文件:"+fDialog.getSelectedFile().getPath();
				     try{
				    	 sci.exec(fDialog.getSelectedFile());
				     }catch(FileNotFoundException fex){
							fex.printStackTrace();
							MessageBox.Show(fex.getMessage());
						}
			    }else
			     msg="文件打开失败！";
			    MessageBox.Show(msg); //提示框
			   }

	};
	private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
		sci.close();
		redirect();
		System.out.println("已清除！");
	}

	private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {

		//��л���У��
		try {
			String selected = jTextArea2.getText();
			if(jTextArea2.getSelectedText()!=null && jTextArea2.getSelectedText().length()>0 )
			selected=	jTextArea2.getSelectedText();
			sci.exec(selected);
			if(selected.lastIndexOf(";")==selected.length()){
				selected = selected.substring(0, selected.length()-1);
			}
			if(selected.lastIndexOf(";")==-1){
				selected=";"+selected;
			}
			String ans = selected.substring(selected.lastIndexOf(";")+1, selected.lastIndexOf("="));
			
			if(sci.isExistingVariable(ans)){
				scitype = sci.get(ans);
				System.out.println(ans+" =");
				System.out.println(scitype.toString());
			}
			
		} catch (JavasciException e) {
			e.printStackTrace();
			MessageBox.Show(e.getLocalizedMessage(), ERROR);
		}
	}
	
//GEN-LAST:event_exitMenuItemActionPerformed

	/**
	 * @param args the command line arguments
	 */
	public static void main(String args[]) {
		java.awt.EventQueue.invokeLater(new Runnable() {
			public void run() {
				new DingY().setVisible(true);
			}
		});
	}

	private MyPrintStream mps;
	private Scilab sci;
	private ScilabType scitype;
	//GEN-BEGIN:variables
	// Variables declaration - do not modify
	private javax.swing.JMenuItem aboutMenuItem;
	private javax.swing.JMenuItem aboutMenuItem1;
	private javax.swing.JMenuItem contentsMenuItem;
	private javax.swing.JMenuItem contentsMenuItem1;
	private javax.swing.JMenuItem exitMenuItem;
	private javax.swing.JMenuItem exitMenuItem1;
	private javax.swing.JMenu fileMenu;
	private javax.swing.JMenu fileMenu1;
	private javax.swing.JMenu helpMenu;
	private javax.swing.JMenu helpMenu1;
	private javax.swing.JButton jButton1;
	private javax.swing.JButton jButton2;
	private javax.swing.JScrollPane jScrollPane1;
	private javax.swing.JScrollPane jScrollPane2;
	private javax.swing.JTextArea jTextArea1;
	private javax.swing.JTextArea jTextArea2;
	private javax.swing.JMenuBar menuBar;
	private javax.swing.JMenuBar menuBar1;
	private javax.swing.JMenuItem openMenuItem;
	private javax.swing.JMenuItem openMenuItem1;
	private javax.swing.JFrame frame;
	// End of variables declaration//GEN-END:variables

}